package com.clever.mall.controller;

import com.clever.mall.commons.dto.UserRequestParam;
import com.clever.mall.commons.dto.WithdrawDepositRequestParam;
import com.clever.mall.commons.exception.UserException;
import com.clever.mall.commons.result.BaseResult;
import com.clever.mall.commons.result.WalletPage;
import com.clever.mall.service.WalletService;
import com.clever.mall.utils.StatusCode;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author ChenWang
 * @className WalletController
 * @date 2020/11/10 10:04
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/wallet")
public class WalletController {
    @Resource
    WalletService walletService;

    /**
     * 进入钱包页面和点击提现都调用这个接口
     * 权限由拦截器或者过滤器解决
     * @param userRequestParam  指定的接口
     * @return      指定的钱包页面信息
     */
    @PostMapping("/detail")
    public BaseResult<WalletPage> walletDetail(@Validated @RequestBody UserRequestParam userRequestParam) {
        WalletPage walletPage = walletService.getMyWalletPage(userRequestParam);
        if(Objects.isNull(walletPage)){
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return BaseResult.success(walletPage);
    }

    /**
     * 提现
     * @param withdrawDepositRequestParam   指定的提现数据
     * @return                              0表示正常，1表示提现有错误
     */
    @PostMapping("/withdrawDeposit")
    public BaseResult<Integer> withdrawDeposit(@Validated@RequestBody WithdrawDepositRequestParam withdrawDepositRequestParam){
        int flag = walletService.withdrawDeposit(withdrawDepositRequestParam);
        return BaseResult.success(flag);
    }
}
